﻿using MediatR;
using Warehouse.API.Applcation.Command.StorehouseCommand;
using Warehouse.Infrstructre.Interface;

namespace Warehouse.API.Applcation.CommandHandlers.StorehouseCommandHandlers
{
    public class CreateStorehouseCommandHandlers:IRequestHandler<CreateStorehouseCommand,int>
    {
        private readonly IStorehouseRepository _storehouse;

        public CreateStorehouseCommandHandlers(IStorehouseRepository storehouse)
        {
            this._storehouse = storehouse;
        }

        public async Task<int> Handle(CreateStorehouseCommand request, CancellationToken cancellationToken)
        {
            var res = await _storehouse.GetAllAsyns();
            if (res.Any(x => x.StorehouseCode == request.StorehouseCode && x.StorehouseName == request.StorehouseName))
            {
                return -1; //已经存在
            }
            if(res.Any(x => x.StorehouseCode == request.StorehouseCode))
            {
                return -2; //编码重复
            }
            return await _storehouse.AddAsync(new Domain.StoreHouse() {  StorehouseId = request.StorehouseId,StorehouseName =request.StorehouseName, StorehouseCode = request.StorehouseCode, StorehouseType = request.StorehouseType, WarehouseId = request.WarehouseId, IsDefaults = request.IsDefaults, IsDisable = request.IsDisable , CreatTime = request.CreatTime, Sremark = request.Sremark, AddName = request.AddName, AddTime = request.AddTime , office_id  = request.office_id, user_id  = request.user_id  });
        }
    }
}
